<?php
/*
 * 车主服务
 * @package
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 75b67c24e00ac624fd77b64a20f0adc78ba14080 $
 */
defined('IN_IXDCW') or exit('Access Denied');
login();
isCompleteData();//检测个人资料是否完善
require DT_CORE.'/'.$module.'/common.inc.php';

$AREA = PCache::read('area.php');
$areaid = Area::area2id(Area::ip2area($DT_IP));
$do = new Business();
$businessAuthDb = Db('businessAuth');
$businessAuth = $do->getAuth($_userid);
if (!$businessAuth['verify']) {
	Prompt::message('您没有权限访问合作商家部分!', url('member/index'));
}
if(empty($action)) {
	$action = 'mainStore';
}

switch($action) {
	case 'addBranchStore':
		$mainStore = $do->get(true);
		$mainStore or Prompt::alert('请先完善总店信息!', 'business.php?action=mainstore');
		if ($post) {
			$do->check($post);
			$post = $do->set($post);
			$do->addBranchStore($post) && Prompt::alert('保存信息成功!');
		}
		break;
	case 'deleteBranchStore':
		$itemid = intval($itemid);
		$itemid or Prompt::alert(ERR_MSG, 'goback');
		$do->delete($itemid);
		Prompt::alert('删除成功!', 'business.php?action=branchStore');
	break;
	case 'listPicture':
		$pictureList = $do->allPicture();
		if ($sub) {
			// 处理上传
			Session::start();
			$oldUrls = (array) __($pictureList)->pluck('thumb');
			$images_url = (array) $images_url;
			MultiUpload::clearDiff('33_multi',(array) $images_url, $oldUrls, $_userid);
			$keyMap = array_combine($images_url, $images_alt);
			foreach ($images_url as $v) {
				$do->updatePicture($v, $keyMap[$v]);
			}
			foreach ($oldUrls as $url) {
				if (!in_array($url, $images_url)) {
					$itemid = 0;
					foreach ($pictureList as $item) {
						if ($url == $item['thumb']) {
							$itemid = $item['itemid'];
						}
					}
					if ($itemid) {
						$do->deletePicture($itemid, true);
					}
				}
			}
			$do->ajaxReturn(array('success'=>1,'message'=>'保存成功'));
		} else {
			include template('business_picture', $module);
			exit;
		}
		break;
	case 'ajaxUploadLicense':
		$license = $_FILES['license'];
		if (!in_array(File::ext($license['name']), array('jpg', 'gif', 'png'))) {
			exit('file_ext_unable');
		}
		if ($license['size']>500*1024) {
			exit('file_size_big');
		}
		// 车主服务 33
		$aim = Picture::name(33, $_userid, File::ext($license['name']));
		$path = Picture::realpath($aim);
		if (File::mkdir(dirname($path))) {
			move_uploaded_file($license['tmp_name'], $path);
			$licenseUrl = Picture::url($path);
			$businessAuthDb->updateLicense($licenseUrl);
			// 已上传等待审核
			$businessAuthDb->updateLicenseStatus($_userid, 2);
			exit('file_upload_success');
		}
		exit('file_error');
		break;
	case 'ajaxAddPicture':
		$do->addPicture($post);
		Prompt::alert('添加成功!', 'business.php?action=listPicture');
		break;
	case 'ajaxDeletePicture':
		$itemid = intval($itemid);
		$itemid or Prompt::alert(ERR_MSG, 'goback');
		$do->deletePicture($itemid);
		Prompt::alert('删除成功!', 'business.php?action=listPicture');
		break;
	case 'editBranchStore':
		$itemid = intval($itemid);
		$itemid or Prompt::alert(ERR_MSG, 'goback');
		if ($post) {
			$do->check($post);
			$do->editBranchStore($itemid, $post);
			Prompt::alert('修改成功!', 'business.php?action=branchStore');
		} else {
			$item = $do->getStore($itemid);
			$mainStore = $do->get(true);
		}

		break;
	case 'branchStore':
		$mainStore = $do->get(true);
		$mainStore or Prompt::alert('请先完善总店信息!', 'business.php');
		$branchStores = $do->getBranchStores();
		$CATEGORY = PCache::read('category-33.php');
		foreach($branchStores as $k => $v) {
			$v['catname'] = $CATEGORY[$v['catid']]['catname'];
			$v['editdate'] = date('Y-m-d', $v['addtime']);
			$branchStores[$k] = $v;
		}
		break;
	case 'mainStore' :
		if ($post) {
			$do->check($post);
			$post = $do->set($post);
			$addReturn  = $do->add($post, true);
			if ( $addReturn === 'firstEdit') {
//				echo '<script> alert("请丰富店铺图片!");parent.window.location = "business.php?action=listPicture";</script>';
//				exit();
			} else if ($addReturn) {
				Prompt::alert('保存店铺信息成功!');
			}
		} else {
			$item = $do->get(true);
		}
		break;
}

include template('business', $module);